var url = decodeURI(location.href);
var pagecount = 5;
var index = 0;
var minChapterId = 0;

function getQueryString(url) {
    var useUrl = url.split("?")[1];
    var params = new Array();
    params = useUrl.split("&");
    var needParams = new Array();
    for (var i = 0; i < params.length; i++) {
        var needParam = params[i].split("=")[1];
        needParams.push(needParam);
    }
    return needParams;
}

$(function () {
    var bookId = getQueryString(url)[0];

    // 获取书本详情
    getBookById(parseInt(bookId));

    // 获取章节目录
    getChapterByBookId(parseInt(bookId));

    // 获取理论前沿
    $("#toXqList").html('<a href="xq_list.html?type=fourBook_liLunQianYan&bookId=' + bookId + '">更多</a>');
    getTheoreticalFrontiers(parseInt(bookId));
})

//获取书本详情
function getBookById(bookId) {
    var data = {
        bookId: bookId
    };
    AjaxSubmit("GET", data, basePath + "/common/resource/getBookById", getBookById_fun);
}
function getBookById_fun(res) {
    var BookLists = res.obj;
    console.log(res);
    if (res.status == "SUCCESS") {
        $(".toptitleimg").find("img").attr("src", BookLists.pic);
        $(".toptitleimg").find(".topp").html(BookLists.bookName);
        $(".toptitleimg").find(".bottomp").html(BookLists.brief);
        $("#home").find(".RightBoxTop").find(".shizheng").find(".xq").html(BookLists.mainPointsOfTeaching);
        $("#fourBookTitle").html(BookLists.bookName);
    }
}

//获取章节目录
function getChapterByBookId(bookId) {
    var data = {
        bookId: bookId
    };
    AjaxSubmit("GET", data, basePath + "/common/resource/getChapterByBookId", getChapterByBookId_fun);
}
function getChapterByBookId_fun(res) {
    var BookChapterLists = res.obj.content;
    if (res.status == "SUCCESS") {
        BookChapterLists = bubbleSort(BookChapterLists);

        minChapterId = BookChapterLists[0].id;

        var chapterSectionHtml = "";

        for (var i = 0; i < BookChapterLists.length; i++) {
            chapterSectionHtml += "<div id='chapter" + BookChapterLists[i].id + "'><li><img style='width: 18px;height: 18px;display: inline-block;position:relative;top: 3px;margin-right: 10px' src='../img/zhankai.jpg' sign='heLong' onclick=zhanKaiJie(this)><a  data-id='" + BookChapterLists[i].id + "' onclick=clickChapter(this) class='chapterA'>" + BookChapterLists[i].title + "</a></li></div>"

            var data = {
                chapterId: BookChapterLists[i].id
            };

            AjaxSubmit("GET", data, basePath + "/common/resource/getSectionByChapterId", getSectionByChapterId_fun, BookChapterLists[i].id + "_" + i);
        }

        $("#ChapterSection").html(chapterSectionHtml);

        // 获取答疑解惑
        if (BookChapterLists.length > 0) {
            getAnswerDoubts(BookChapterLists[0].id);

            // 获取教学案例
            $("#toXqListJxal").html('<a href="xq_list.html?type=fourBook_jiaoXueAnLi&chapterId=' + BookChapterLists[0].id + '">更多</a>');
            getNewsJiaoXueAnLi(parseInt(BookChapterLists[0].id));
        } else {
            $("#answerDoubts").html("");
        }
    }
}
function getSectionByChapterId_fun(res, chapterId) {
    var ChapterSectionLists = res.obj.content;
    if (res.status == "SUCCESS") {
        ChapterSectionLists = bubbleSort(ChapterSectionLists);

        var sectionChapterIdHtml = "<div class='mySection' style='display: none;padding-left: 30px'>";

        for (var i = 0; i < ChapterSectionLists.length; i++) {
            sectionChapterIdHtml += "<a><li class='smallList' data-id='" + ChapterSectionLists[i].id + "' onclick=clickSection(this)>" + ChapterSectionLists[i].title + "</li></a>";
        }

        sectionChapterIdHtml += "</div>";

        $("#chapter" + chapterId.split("_")[0]).append(sectionChapterIdHtml);

        if (ChapterSectionLists.length != 0 && chapterId.split("_").length > 1 && minChapterId == parseInt(chapterId.split("_")[0])) {
            if ($("#chapter" + chapterId.split("_")[0]).find("li").length > 1) {
                $("#chapter" + chapterId.split("_")[0]).find("li").eq(1).addClass("active");
            }

            // 获取视频素材
            $("#toXqListSpsc").html('<a href="xq_list.html?type=fourBook_shiPinSuCai&sectionId=' + ChapterSectionLists[0].id + '">更多</a>');
            getResourceShiPinSuCai(ChapterSectionLists[0].id);

            // 获取文档素材
            $("#toXqListWdsc").html('<a href="xq_list.html?type=fourBook_wenDangSuCai&sectionId=' + ChapterSectionLists[0].id + '">更多</a>');
            searchCommonResourceWenDangSuCai(ChapterSectionLists[0].id);

            // 获取其他素材
            $("#toXqListQtsc").html('<a href="xq_list.html?type=fourBook_qiTaSuCai&sectionId=' + ChapterSectionLists[0].id + '">更多</a>');
            searchCommonResourceQiTaSuCai(ChapterSectionLists[0].id);

            // 获取精彩一课
            $("#toXqListJcyk").html('<a href="xq_list.html?type=fourBook_jingCaiYiKe&sectionId=' + ChapterSectionLists[0].id + '">更多</a>');
            searchCommonResourceJingCaiYiKe(ChapterSectionLists[0].id);
        }
    }
}

//展开节
function zhanKaiJie(btn) {
    $(btn).parent().next().slideToggle(500);

    if ($(btn).attr("sign") == "heLong") {
        $(btn).attr("src", "../img/helong.jpg");
        $(btn).attr("sign", "zhanKai");
    } else {
        $(btn).attr("src", "../img/zhankai.jpg");
        $(btn).attr("sign", "heLong");
    }
}
function clickChapter(btn) {
    $("#ChapterSection").find(".chapterA").removeClass("active");
    $(btn).addClass("active");

    // 获取答疑解惑
    getAnswerDoubts($(btn).attr("data-id"));

    // 获取视频素材
    $("#toXqListSpsc").html('<a href="xq_list.html?type=fourBook_shiPinSuCai&chapterId=' + $(btn).attr("data-id") + '">更多</a>');
    getResourceShiPinSuCaiByChapterId($(btn).attr("data-id"));

    // 获取文档素材
    $("#toXqListWdsc").html('<a href="xq_list.html?type=fourBook_wenDangSuCai&chapterId=' + $(btn).attr("data-id") + '">更多</a>');
    searchCommonResourceWenDangSuCaiByChapterId($(btn).attr("data-id"));

    // 获取其他素材
    $("#toXqListQtsc").html('<a href="xq_list.html?type=fourBook_qiTaSuCai&chapterId=' + $(btn).attr("data-id") + '">更多</a>');
    searchCommonResourceQiTaSuCaiByChapterId($(btn).attr("data-id"));

    // 获取精彩一课
    $("#toXqListJcyk").html('<a href="xq_list.html?type=fourBook_jingCaiYiKe&chapterId=' + $(btn).attr("data-id") + '">更多</a>');
    searchCommonResourceJingCaiYiKeByChapterId($(btn).attr("data-id"));

    // 获取教学案例
    $("#toXqListJxal").html('<a href="xq_list.html?type=fourBook_jiaoXueAnLi&chapterId=' + $(btn).attr("data-id") + '">更多</a>');
    getNewsJiaoXueAnLi(parseInt($(btn).attr("data-id")));
}
function clickSection(btn) {
    $("#ChapterSection").find("li").removeClass("active");
    $(btn).addClass("active");

    var sectionId = $(btn).attr("data-id");

    // 获取视频素材
    $("#toXqListSpsc").html('<a href="xq_list.html?type=fourBook_shiPinSuCai&sectionId=' + sectionId + '">更多</a>');
    getResourceShiPinSuCai(sectionId);

    // 获取文档素材
    $("#toXqListWdsc").html('<a href="xq_list.html?type=fourBook_wenDangSuCai&sectionId=' + sectionId + '">更多</a>');
    searchCommonResourceWenDangSuCai(sectionId);

    // 获取其他素材
    $("#toXqListQtsc").html('<a href="xq_list.html?type=fourBook_qiTaSuCai&sectionId=' + sectionId + '">更多</a>');
    searchCommonResourceQiTaSuCai(sectionId);

    // 获取精彩一课
    $("#toXqListJcyk").html('<a href="xq_list.html?type=fourBook_jingCaiYiKe&sectionId=' + sectionId + '">更多</a>');
    searchCommonResourceJingCaiYiKe(sectionId);
}

//获取理论前沿
function getTheoreticalFrontiers() {
    var data = {
        page: index,
        commonNewsTypeId: 8,
        size: pagecount
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/getNews", getTheoreticalFrontiers_fun);
}
function getTheoreticalFrontiers_fun(res) {
    var resultData = res.obj.content;

    var theoreticalFrontiersHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        theoreticalFrontiersHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','fourBook_jiaoXueAnLi')><li class='clearfix'>";
        theoreticalFrontiersHtml += "<img src='" + resultData[i].pic + "' alt='' class='img-rounded pull-left'>";
        theoreticalFrontiersHtml += "<div class='pull-left right-list'>";
        theoreticalFrontiersHtml += "<p>" + resultData[i].title + "</p>";
        theoreticalFrontiersHtml += "<p class='small'>";
        theoreticalFrontiersHtml += "<span class='longsmall pull-left'>" + resultData[i].abstructContent + "</span>";
        theoreticalFrontiersHtml += "<span class='pull-right' style='margin-left:50px;'>浏览" + resultData[i].pageView + "次</span>";
        theoreticalFrontiersHtml += "<span class='pull-right'>" + changeTime(resultData[i].creatTime) + "</span>";
        theoreticalFrontiersHtml += "</p>";
        theoreticalFrontiersHtml += "</div>";
        theoreticalFrontiersHtml += "</li></a>";
    }

    $("#theoreticalFrontiers").html(theoreticalFrontiersHtml);
}

// 获取答疑解惑
function getAnswerDoubts(chapterId) {
    var data = {
        chapterId: chapterId,
        type: "BOOK",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/getAnswerDoubts", getAnswerDoubts_fun);
}
function getAnswerDoubts_fun(res) {
    var resultData = res.obj.content;

    var answerDoubtsHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        answerDoubtsHtml += "<div class='question border-bottom'>";
        answerDoubtsHtml += "<p class='problem'>Q：" + resultData[i].question + "</p>";
        answerDoubtsHtml += "<p class='answer'>A：" + resultData[i].answer + "</p></div>";
    }

    $("#answerDoubts").html(answerDoubtsHtml);
}

// 获取视频素材
function getResourceShiPinSuCai(sectionId) {
    var data = {
        sectionId: sectionId,
        type: "VIDEO_MATERIAL",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", getResourceShiPinSuCai_fun);
}
function getResourceShiPinSuCaiByChapterId(chapterId) {
    var data = {
        chapterId: chapterId,
        type: "VIDEO_MATERIAL",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", getResourceShiPinSuCai_fun);
}
function getResourceShiPinSuCai_fun(res) {
    var resultData = res.obj.content;

    var resourceShiPinSuCaiHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        if (resultData[i].field4 == "1") {
            resourceShiPinSuCaiHtml += "<a href='" + resultData[i].fileUrl + "'><div class='imgBoxList'>";
        } else {
            resourceShiPinSuCaiHtml += "<a onclick=toXqVideo('" + resultData[i].id + "','fourBook_shiPinSuCai')><div class='imgBoxList'>";
        }
        resourceShiPinSuCaiHtml += "<img src='" + resultData[i].pic + "' alt='' style='width:56px;height:76px;'>";
        resourceShiPinSuCaiHtml += "<p>" + limitLength(34, resultData[i].title) + "</p>";
        resourceShiPinSuCaiHtml += "</div></a>";
    }

    $("#resourceShiPinSuCai").html(resourceShiPinSuCaiHtml);
}

// 获取文档素材
function searchCommonResourceWenDangSuCai(sectionId) {
    var data = {
        sectionId: sectionId,
        type: "DOCUMENT_MATERIAL",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", searchCommonResourceWenDangSuCai_fun);
}
function searchCommonResourceWenDangSuCaiByChapterId(chapterId) {
    var data = {
        chapterId: chapterId,
        type: "DOCUMENT_MATERIAL",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", searchCommonResourceWenDangSuCai_fun);
}
function searchCommonResourceWenDangSuCai_fun(res) {
    var resultData = res.obj.content;

    var commonResourceWenDangSuCaiHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        commonResourceWenDangSuCaiHtml += "<div class='col-md-6 border-bottom '>";
        commonResourceWenDangSuCaiHtml += "<img src='../img/blue.png' alt='' class='pull-left' >";
        commonResourceWenDangSuCaiHtml += "<p class='wordname'><a href='" + resultData[i].fileUrl + "'  target='_blank'>" + limitLength(30, resultData[i].title) + "</a></p>";
        commonResourceWenDangSuCaiHtml += "</div>";
    }

    $("#commonResourceWenDangSuCai").html(commonResourceWenDangSuCaiHtml);
}

// 获取其他素材
function searchCommonResourceQiTaSuCai(sectionId) {
    var data = {
        sectionId: sectionId,
        type: "OTHER_MATERIAL",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", searchCommonResourceQiTaSuCai_fun);
}
function searchCommonResourceQiTaSuCaiByChapterId(chapterId) {
    var data = {
        chapterId: chapterId,
        type: "OTHER_MATERIAL",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", searchCommonResourceQiTaSuCai_fun);
}
function searchCommonResourceQiTaSuCai_fun(res) {
    var resultData = res.obj.content;

    var commonResourceQiTaSuCaiHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        commonResourceQiTaSuCaiHtml += "<div class='col-md-6 border-bottom '>";
        commonResourceQiTaSuCaiHtml += "<img src='../img/blue.png' alt='' class='pull-left'>";
        commonResourceQiTaSuCaiHtml += "<p class='wordname'><a href='" + resultData[i].fileUrl + "'  target='_blank'>" + limitLength(30, resultData[i].title) + "</a></p>";
        commonResourceQiTaSuCaiHtml += "</div>";
    }

    $("#commonResourceQiTaSuCai").html(commonResourceQiTaSuCaiHtml);
}

// 获取精彩一课
function searchCommonResourceJingCaiYiKe(sectionId) {
    var data = {
        sectionId: sectionId,
        type: "WONDERFUL_LESSON",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", searchCommonResourceJingCaiYiKe_fun);
}
function searchCommonResourceJingCaiYiKeByChapterId(chapterId) {
    var data = {
        chapterId: chapterId,
        type: "WONDERFUL_LESSON",
        page: 0,
        size: 5
    };

    AjaxSubmit("GET", data, basePath + "/common/resource/searchCommonResource", searchCommonResourceJingCaiYiKe_fun);
}
function searchCommonResourceJingCaiYiKe_fun(res) {
    var resultData = res.obj.content;

    var commonResourceJingCaiYiKeHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        if (resultData[i].field4 == "1") {
            commonResourceJingCaiYiKeHtml += '<a href="' + resultData[i].fileUrl + '"><div class="imgBoxList">';
        } else {
            commonResourceJingCaiYiKeHtml += '<a onclick=toXqVideo("' + resultData[i].id + '","fourBook_jingCaiYiKe")><div class="imgBoxList">';
        }
        commonResourceJingCaiYiKeHtml += '<img src="' + resultData[i].pic + '" alt="">';
        commonResourceJingCaiYiKeHtml += '<p>' + limitLength(24, resultData[i].title) + '</p>';
        commonResourceJingCaiYiKeHtml += '<p class="time">' + toHourMinute(resultData[i].videoTime) + '</p>';
        commonResourceJingCaiYiKeHtml += '</div></a>';
    }

    $("#commonResourceJingCaiYiKe").html(commonResourceJingCaiYiKeHtml);
}

// 获取教学案例
function getNewsJiaoXueAnLi(chapterId) {
    var data = {
        chapterId: chapterId,
        commonNewsTypeId: 7,
        status: "UP",
        page: 0,
        size: 5
    }

    AjaxSubmit("GET", data, basePath + "/common/resource/getNews", getNewsJiaoXueAnLi_fun);
}
function getNewsJiaoXueAnLi_fun(res) {
    var resultData = res.obj.content;

    var newsJiaoXueAnLiHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        newsJiaoXueAnLiHtml += '<a onclick=toXqNormal("' + resultData[i].id + '","fourBook_jiaoXueAnLi")><li class="clearfix">';
        newsJiaoXueAnLiHtml += '<img style="display:none;" src="' + resultData[i].pic + '" alt="" class="img-rounded pull-left">';
        newsJiaoXueAnLiHtml += '<div class="pull-left right-list">';
        newsJiaoXueAnLiHtml += '<p>' + resultData[i].title + '</p>';
        newsJiaoXueAnLiHtml += '<p class="small">';
        newsJiaoXueAnLiHtml += '<span class="longsmall pull-left">' + resultData[i].abstructContent + '</span>';
        newsJiaoXueAnLiHtml += '<span class="pull-right" style="margin-left:50px;">浏览' + resultData[i].pageView + '次</span>';
        newsJiaoXueAnLiHtml += '<span class="pull-right">' + changeTime(resultData[i].creatTime) + '</span>';
        newsJiaoXueAnLiHtml += '</p>';
        newsJiaoXueAnLiHtml += '</div>'
        newsJiaoXueAnLiHtml += '</li></a>';
    }

    $("#newsJiaoXueAnLi").html(newsJiaoXueAnLiHtml);
}

// 冒泡排序
function bubbleSort(arr) {
    var len = arr.length;
    for (var i = 0; i < len; i++) {
        for (var j = 0; j < len - 1 - i; j++) {
            if (arr[j].num > arr[j + 1].num) {        // 相邻元素两两对比
                var temp = arr[j + 1];        // 元素交换
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}

// 跳转至详情
function toXqNormal(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    self.location.href = "xq.html";
}

// 跳转至视频
function toXqVideo(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    window.open("video.html");
}

//限制字符串长度
function limitLength(wantLength, str) {
    var returnStr = "";
    var realLength = 0;
    var len = (str != null) ? str.length : 0;
    var charCode = -1;
    for (var i = 0; i < len; i++) {
        if (realLength < wantLength) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128)
                realLength += 1;
            else
                realLength += 2;
            returnStr += str.charAt(i);
        } else {
            returnStr += "...";
            break;
        }
    }
    return returnStr;
}